import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class PartyHandParser extends HandParser {
	
	public PartyHandParser(String uri, String name) {
		super(uri,name);
	}

	static String str="***** Hand History for Game";
	@Override
	public GameInfo ReadFile(String filename) {
		GameInfo gi=new GameInfo();
		String file=null;
		try {
			file=FileHandling.FiletoString(filename);
		} catch (IOException e) {
			e.printStackTrace();
		}

		if(file.contains("Buy-in Trny:")==false)
			return null;
		
		//sdate
		gi.date=Calendar.getInstance();
		int start=file.indexOf("\n", file.indexOf(str));
		int end=file.indexOf("\n", start+1);
		String header=file.substring(start+1, end+1);
		String month=null;
		header=header.substring(header.indexOf(",")+2,header.length()-2);
		month=header.substring(0, header.indexOf(" "));
		header=header.substring(header.indexOf(" ")+1);
		DateFormat df=new SimpleDateFormat("dd, hh:mm:ss Z y");
		try {																												
			gi.date.setTime(df.parse(header));
			gi.date.add(Calendar.MONTH,MonthtoInt(month));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		//edate
		gi.edate=Calendar.getInstance();
		start=file.indexOf("\n", file.lastIndexOf(str));
		end=file.indexOf("\n", start+1);
		header=file.substring(start+1, end+1);
		header=header.substring(header.indexOf(",")+2,header.length()-2);
		month=header.substring(0, header.indexOf(" "));
		header=header.substring(header.indexOf(" ")+1);
		try {																												
			gi.edate.setTime(df.parse(header));
			gi.edate.add(Calendar.MONTH,MonthtoInt(month));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		//gamenum
		header=file.substring(0, file.indexOf("\n"));
		header=header.substring(header.indexOf("#")+1, header.lastIndexOf(" "));
		gi.gamenum=Long.parseLong(header);
		
		//Buy-in
		header=file.substring(file.indexOf("$")+1,file.indexOf("USD")-1);
		switch(Integer.parseInt(header))
		{
		case 1:
		{
			gi.buyin=0.8;
			gi.rake=0.2;
			break;
		}
		case 3:
		{
			gi.buyin=2.4;
			gi.rake=0.6;
			break;
		}
		case 6:
		{
			gi.buyin=5;
			gi.rake=1;
			break;
		}	
		case 11:
		{
			gi.buyin=10;
			gi.rake=1;
			break;
		}
		case 22:
		{
			gi.buyin=20;
			gi.rake=2;
			break;
		}
		case 33:
		{
			gi.buyin=30;
			gi.rake=3;
			break;
		}
		case 55:
		{
			gi.buyin=50;
			gi.rake=5;
			break;
		}
		case 109:
		{
			gi.buyin=100;
			gi.rake=9;
			break;
		}
		}
		
		//players
		header=file.substring(file.indexOf(":",file.indexOf("Total number of players : ")+1)+2,
				file.indexOf("/",file.indexOf("Total number of players : ")));
		gi.players=Short.parseShort(header);
		
																								
		//finishing
		header=file.substring(0, file.indexOf("[")-1);
		header=header.substring(header.lastIndexOf(" ")+1);
		try
		{
			header=file.substring(file.indexOf(header+" finished in"));
			header=header.substring(header.indexOf("in ")+"in".length()+1,header.lastIndexOf(" place"));
			gi.finishing=Short.parseShort(header.charAt(0)+"");
		}
		catch(Exception e)
		{
			gi.finishing=1;
		}
											
		//winnings
		header=file.substring(0, file.indexOf("[")-1);
		header=header.substring(header.lastIndexOf(" ")+1);
		try
		{
			header=file.substring(file.indexOf(header+" finished in"));
			header=header.substring(header.lastIndexOf("and won ")+"and won $".length(),header.lastIndexOf(" "));
			gi.winnings=Double.parseDouble(header);
		}
		catch(Exception e)
		{
			if(gi.finishing==1)
				switch(gi.players)
				{
				case 2:
					gi.winnings=2*gi.buyin;
					break;
				case 6:
					gi.winnings=6*gi.buyin*0.6;
					break;
				case 10:
					gi.winnings=10*gi.buyin*0.5;
					break;
				}
		}
		
		//duration
		gi.duration=Calendar.getInstance();
		long diff=gi.edate.getTimeInMillis()-gi.date.getTimeInMillis();
																																																			
		gi.duration.setTimeInMillis(diff);
		
		//roomname
		gi.roomname=this.getName();
		
		return gi;
	}

	@Override
	public ArrayList<GameInfo> Reader() {
		ArrayList<GameInfo> lista=new ArrayList<GameInfo>();
		File maindir=new File(Uri);
		
		try {
			for(String s: maindir.list())
			{
				File subdir=new File(maindir.getPath()+"/"+s);
				for(String f: subdir.list())
					lista.add(this.ReadFile(subdir.getPath()+"/"+f));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for(int i=0;i<lista.size();i++)
			if(lista.get(i)==null)
				lista.remove(i);
		
		return lista;
	}
}


